//后台用户列表
$(function () {
    /**
     * 体测评分
     */
    $('#manager-admin').datagrid({
        url: Think.APP + "/AdminUser/admin_list", //数据源
        fit: true, //自适应
        striped: true,
        fitColumns: true,
        toolbar: '#admin-toolbar',
        border: false, //取消边框
        pagination: true, //分页开启
        pageSize: 50,
        rownumbers: true,
        columns: [[
                 {title: 'ID', field: 'id',checkbox: true,formatter:function(value,rec){
                  alert(123);
                  console.log(value);
                    if('rec.name'=='admin'){
                         $("input[name='id']").removeAttr('checkbox');
                    }
                 }},
                {field: 'name', title: '用户名', width: 100, align: 'center'},
                // {field: 'name_url', title: '菜单链接', width: 100, align: 'center'},
                {field: 'add_time', title: '添加时间', width: 100, align: 'center'},
                {field: 'last_login_time', title: '最后登录', width: 100, align: 'center'},
                {field: 'last_login_ip', title: '最后登录ip', width: 100, align: 'center'},
                {field: 'is_activity', title: '是否有效', width: 50, align: 'center'},
                {field: 'action', title:'操作',  width:100, align:'center',formatter:function(value,rec){
                  if(rec.name != 'admin'){
                    var btn = '<a class="hand_import_action" onclick="admin_toolbar.select(\''+rec.id+'\',\''+rec.name+'\')" href="javascript:void(0)">查看权限</a>|'+
                          '<a class="auto_import_action" onclick="admin_toolbar.add(\''+rec.id+'\',\''+rec.name+'\')" href="javascript:void(0)">权限分配</a>';
                   return btn;
                  }

                }}
            ]],
/*            onClickRow: function(rowIndex, rowData) {//单击事件
                $('#manager-scope-import').datagrid("clearChecked"); //取消所有行的选中
                $('#manager-scope-import').datagrid("selectRow", rowIndex);  //选中当前行
            },*/
             onLoadSuccess:function(data){
               $('.hand_import_action').linkbutton({text:'查看权限',plain:true});
               $('.auto_import_action').linkbutton({text:'权限分配',plain:true});
           },
    });




    //显示添加框
    function add_menu_dialog(data,name){
    $('#import-add-form').dialog({
        title: name+' 添加子菜单',
        width: 500,
        closed: true,
        modal: true, //遮罩层
        buttons: [{
                text: "确 定",
                handler: function () {
                    //获取数据
                    $('#import-add-form').form({
                        url: Think.APP + "/Nav/add_menu?id="+data,
                        dataType: "json",
                        success: function (data) {
                          //  var data = eval('(' + data + ')');
                          var data = $.parseJSON(data);
                            if (data.success === 1) {
                               $.messager.alert('提示', data.msg, 'success');
                                //刷新
                                $('#import-add-form').dialog("close").form("reset");  //关闭窗体
                                $('#manager-scope-import').datagrid("reload");  //重新加载数据

                            } else
                            {
                                //添加失败
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        }
                    });
                    //提交表单
                    $('#import-add-form').submit();
                }
            }, {
                text: "取 消",
                handler: function () {
                    //关闭
                    $('#import-add-form').dialog("close").form('reset');
                }
            }]
    });

      $('#import-add-form').dialog("open");//开启
}
    //导入工具条
    admin_toolbar = {
        add: function (data,name) {  //添加
            //弹出dialog
            open_nav_dialog_add(data,name);

        },
        select:function(data,name){  //查看子菜单
          //弹出dialog
          open_nav_dialog(data,name);

        },
        select_son:function(data){  //查看子菜单
          //弹出dialog
          //console.log(data);
          get_menu_list(data);

        }
    }

    function open_nav_dialog(data,name){
      //显示
      debugger;
      var id = data;
      $('#nav-select-form').dialog({
          title: name+ "查看权限",
          width: 400,
          height:500,
          closed: true,
          modal: true, //遮罩层
          buttons: [{
                  text: "取 消",
                  handler: function () {
                      //关闭
                      $('#nav-select-form').dialog("close");
                  }

              }]
      });
      $('#nav-select-form').dialog("open");
      admin_tree(data);
    }

//分类权限
    function open_nav_dialog_add(data,name){
      //显示
      var id = data;
      $('#nav-select-form').dialog({
          title: name+ "查看权限",
          width: 400,
          height:500,
          closed: true,
          modal: true, //遮罩层
          buttons: [{
                  text: "取 消",
                  handler: function () {
                      //关闭
                      $('#nav-select-form').dialog("close");
                  }

              },{
              text: "保 存",
              handler: function () {
                  //保存checkbox 选中的权限
                  //获取到所有被选中的权限
                  var node = $('#select_nav').tree('getSelected');
console.log(node.text);
              // console.log($('#select_nav').tree('getSelected').id)  ;
              }
            }]
      });
      $('#nav-select-form').dialog("open");
      admin_tree_add(data);
    }

function admin_tree(data){
  var id = data;
  $('#select_nav').tree({
    url : Think.APP+'/AdminUser/sel_nav?id='+id,
    lines : true,
  });
}


//显示checkbox tree
function admin_tree_add(data){
  var id = data;
  $('#select_nav').tree({
    url : Think.APP+'/AdminUser/sel_nav_add?id='+id,
    lines : true,
    checkbox: true,
    cascadeCheck: false,
    onLoadSuccess:function(){
      //  if($('#select_nav').val())
      // $('#select_nav').after('<button id="add_button">保存</button>');
    }

  });
}



});
